package com.jml.队列和线程池;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author:jml
 * @date: 2019/5/6 15:01
 * @description：
 */
class MyThread implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println("come in callable .....");
        Thread.sleep(2000);
        return 1024;
    }
}

public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask<>(new MyThread());
        //多个线程抢一个futureTask， 只有一个线程执行，其他只是复用，不会再进入运行，如果想都执行，就都new一个futureTask
        new Thread(futureTask, "AA").start();
        new Thread(futureTask, "BB").start();
        int result01 = 100;
        //自旋锁等待callable计算完毕
        while(!futureTask.isDone()){

        }
        int result02 = futureTask.get();
        System.out.println(Thread.currentThread().getName()+":得到返回值result "+futureTask.get());
        System.out.println(result01+result02);
    }
}
